const PROMISE_STATUS_PENDING = "pending";
const PROMISE_STATUS_FULFILLED = "fulfilled";
const PROMISE_STATUS_REJECT = "rejected";

class HSPromise {
    constructor(executor) {
        this.status = PROMISE_STATUS_PENDING;
        this.value = undefined
        this.reason = undefined

        const resolve = (value) => {
            if (this.status === PROMISE_STATUS_PENDING) {
                this.status = PROMISE_STATUS_FULFILLED;
                queueMicrotask(() => {
                    this.value = value
                    console.log("resolve被调用");
                    // then 传进来的第一个回调函数
                    this.onfulfilled(this.value)
                })
            }
        };
        const reject = (reason) => {
            if (this.status === PROMISE_STATUS_PENDING) {
                this.status = PROMISE_STATUS_REJECT;
                queueMicrotask(() => {
                    this.reason = reason
                    console.log("reject被调用");
                    // then 传进来的第二个回调函数
                    this.onrejected(this.reason)
                })

            }
        };
        executor(resolve, reject);
    }

    then(onfulfilled, onrejected) {
        this.onfulfilled = onfulfilled
        this.onrejected = onrejected
    }
}

const promise = new HSPromise((resolve, reject) => {
    console.log("状态pending");
    resolve(11112222);
    reject(22222);
});

promise.then(res => {
        console.log(res)
    },
    err => {
console.log(err)
    })

    promise.then(res => {
        console.log(res)
    },
    err => {
console.log(err)
    })